package dw.fastfile.common.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wanghao
 * @create: 2022-12-08 11:07
 **/
public class ExcelUtil {
    public static void write(OutputStream outputStream, Integer sheetNo, List<?> list, Map<String, Object> map, String filename, InputStream inputStream){
        ExcelWriter excelWriter = null;
        try {

            excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build();
            WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo).build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(list, fillConfig, writeSheet);
            excelWriter.fill(map, writeSheet);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            excelWriter.finish();
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//    public void fillReportWithEasyExcel(HttpServletResponse response, Integer sheetNo, List<?> list, Map<String, String> map, String filename, InputStream inputStream){
//        ExcelWriter excelWriter = null;
//        try {
//            OutputStream outputStream = response.getOutputStream();
//            response.setHeader("Content-disposition", "attachment; filename=" + filename);
//            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
//            response.setHeader("Pragma", "No-cache");//设置头
//            response.setHeader("Cache-Control", "no-cache");//设置头
//            response.setDateHeader("Expires", 0);//设置日期头
//            excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build();
//            WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo).build();
//            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
//            excelWriter.fill(list, fillConfig, writeSheet);
//            excelWriter.fill(map, writeSheet);
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            excelWriter.finish();
//            try {
//                inputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
